// The Grid component allows an element to be located
//  on a grid of tiles

//stores the users score
var score=0;
var time=20;
var stage=1;
var elapsedTime=0;
var stageName = "Field";

var yellowCoinsCollected=0;
var redCoinsCollected=0;
var greenCoinsCollected=0;
var blueCoinsCollected=0;

var bootsCollected=0;

var boostDuration = 5;
var speedBoost = false;

var clocksCollected = 0;

var multipliersCollected = 0;

var firesCollected = 0;

var multiplierDuration = 5;
var multiplierOn = false;

var yellowCoin = 100;
var redCoin = 200;
var greenCoin = 400;
var blueCoin = 1000;

var playerSpeed = 2;

var treeSprite = 'spr_tree3';
var bushSprite = 'spr_bush2';
var rockSprite = 'spr_rock8';

var backgroundColor = 'rgb(68,132,14)';

Crafty.c('Grid', {
  init: function() {
    this.attr({
      w: Game.map_grid.tile.width,
      h: Game.map_grid.tile.height
    })
  },
 
  // Locate this entity at the given position on the grid
  at: function(x, y) {
    if (x === undefined && y === undefined) {
      return { x: this.x/Game.map_grid.tile.width, y: this.y/Game.map_grid.tile.height }
    } else {
      this.attr({ x: x * Game.map_grid.tile.width, y: y * Game.map_grid.tile.height });
      return this;
    }
  }
});
 
// An "Actor" is an entity that is drawn in 2D on canvas
//  via our logical coordinate grid
Crafty.c('Actor', {
  init: function() {
    this.requires('2D, Canvas, Grid');
  },
});
 
// A Tree is just an Actor with a certain sprite
Crafty.c('Tree', {
  init: function() {
    this.requires('Actor, Solid,' + treeSprite);
  },
});
 
// A Bush is just an Actor with a certain sprite
Crafty.c('Bush', {
  init: function() {
    this.requires('Actor, Solid,' + bushSprite);
  },
  //remove: function() {
  //  this.destroy();
  //}
});
 
// A Rock is just an Actor with a certain sprite
Crafty.c('Rock', {
  init: function() {
    this.requires('Actor, Solid,' + rockSprite);
  },
  //remove: function() {
  //  this.destroy();
  //}
});


/*// This is the enemy character
Crafty.c('Enemy', {
  init: function() {
    this.requires('Actor, Solid, spr_enemy, SpriteAnimation')
      .animate('EnemyMovingUp',    0, 0, 2)
      .animate('EnemyMovingRight', 0, 1, 2)
      .animate('EnemyMovingDown',  0, 2, 2)
      .animate('EnemyMovingLeft',  0, 3, 2);
 
    // Watch for a change of direction and switch animations accordingly
    var animation_speed = 8;
 //   x=-1;
   // y=0;

   this.bind('TempTimer', function(x,y) {
      if (x > 0) {
        this.animate('EnemyMovingRight', animation_speed, -1);
      } else if (x < 0) {
        this.animate('EnemyMovingLeft', animation_speed, -1);
      } else if (y > 0) {
        this.animate('EnemyMovingDown', animation_speed, -1);
      } else if (y < 0) {
        this.animate('EnemyMovingUp', animation_speed, -1);
      } else {
        this.stop();
      }
    });
  },
  
  // Registers a stop-movement function to be called when
  //  this entity hits an entity with the "Solid" component
  stopOnSolids: function() {
    this.onHit('Solid', this.stopMovement);
    return this;
  },
 
  // Stops the movement
  stopMovement: function() {
    this._speed = 0;
    if (this._movement) {
      this.x -= this._movement.x;
      this.y -= this._movement.y;
    }
  },
  
})*/;
 
// This is the player-controlled character
Crafty.c('PlayerCharacter', {
  init: function() {
    this.requires('Actor, Fourway, Collision, spr_player, SpriteAnimation')
      .fourway(playerSpeed)
      .stopOnSolids()
      .onHit('Village', this.visitVillage)
      .onHit('YellowCoin', this.collectYellowCoin)
      .onHit('RedCoin', this.collectRedCoin)
      .onHit('GreenCoin', this.collectGreenCoin)
      .onHit('BlueCoin', this.collectBlueCoin)
      .onHit('Boots', this.collectBoots)
      .onHit('Flame', this.collectFlame)
      .onHit('Clock', this.collectClock)
      .onHit('Multiplier', this.collectMultiplier)
      // These next lines define our four animations
      //  each call to .animate specifies:
      //  - the name of the animation
      //  - the x and y coordinates within the sprite
      //     map at which the animation set begins
      //  - the number of animation frames *in addition to* the first one
      .animate('PlayerMovingUp',    0, 0, 2)
      .animate('PlayerMovingRight', 0, 1, 2)
      .animate('PlayerMovingDown',  0, 2, 2)
      .animate('PlayerMovingLeft',  0, 3, 2);
 
    // Watch for a change of direction and switch animations accordingly
    var animation_speed = 4;
    this.bind('NewDirection', function(data) {
      if (data.x > 0) {
        this.animate('PlayerMovingRight', animation_speed, -1);
      } else if (data.x < 0) {
        this.animate('PlayerMovingLeft', animation_speed, -1);
      } else if (data.y > 0) {
        this.animate('PlayerMovingDown', animation_speed, -1);
      } else if (data.y < 0) {
        this.animate('PlayerMovingUp', animation_speed, -1);
      } else {
        this.stop();
      }
    });
  },
 
  // Registers a stop-movement function to be called when
  //  this entity hits an entity with the "Solid" component
  stopOnSolids: function() {
    this.onHit('Solid', this.stopMovement);
 
    return this;
  },
 
  // Stops the movement
  stopMovement: function() {
    this._speed = 0;
    if (this._movement) {
      this.x -= this._movement.x;
      this.y -= this._movement.y;
    }
  },
 
  // Respond to this player visiting a village
    visitVillage: function(data) {
    villlage = data[0].obj;
    //increment users score
    score=parseInt(score+10000);
    document.getElementById("score").innerHTML="Score: "+score;
    //call function
    villlage.visit();
  },
  
    collectYellowCoin: function(data) {
    coin = data[0].obj;
    coin.collect();
    //increment users score
    score = parseInt(score+yellowCoin);
    yellowCoinsCollected=parseInt(yellowCoinsCollected)+1;
    document.getElementById("score").innerHTML="Score: "+score;
  },
  
    collectRedCoin: function(data) {
    coin = data[0].obj;
    coin.collect();
    //increment users score
    score = parseInt(score+redCoin);
    redCoinsCollected=parseInt(redCoinsCollected)+1;
    document.getElementById("score").innerHTML="Score: "+score;
  },
  
    collectGreenCoin: function(data) {
    coin = data[0].obj;
    coin.collect();
    //increment users score
    score = parseInt(score+greenCoin);
    greenCoinsCollected=parseInt(greenCoinsCollected)+1;
    document.getElementById("score").innerHTML="Score: "+score;
  },
  
    collectBlueCoin: function(data) {
    coin = data[0].obj;
    coin.collect();
    //increment users score
    score = parseInt(score+blueCoin);
    blueCoinsCollected=parseInt(blueCoinsCollected)+1;
    document.getElementById("score").innerHTML="Score: "+score;
  },
  
    collectBoots: function(data) {
    boots = data[0].obj;
    boots.collect();
    bootsCollected=parseInt(bootsCollected)+1;
  },
  
    collectClock: function(data) {
    clock = data[0].obj;
    clock.collect();
    //increment users' clock total
    clocksCollected=parseInt(clocksCollected)+1;
  },
  
    collectFlame: function(data) {
    flame = data[0].obj;
    flame.collect();
    //increment users' flame total
    firesCollected=parseInt(firesCollected)+1;
  },
  
    collectMultiplier: function(data) {
    multiplier = data[0].obj;
    multiplier.collect();
    //increment users' multiplyer total
    multipliersCollected=parseInt(multipliersCollected)+1;
  },
  
});
 
// A village is a tile on the grid that the PC must visit in order to win the game
Crafty.c('Village', {
  init: function() {
    this.requires('Actor, spr_village');
  },
 
  // Process a visitation with this village
  visit: function() {
    this.destroy();
    Crafty.audio.play('knock');
    Crafty.trigger('VillageVisited', this);
  }
});


// A village is a tile on the grid that the PC must visit in order to win the game
Crafty.c('YellowCoin', {
  init: function() {
    this.requires('Actor, spr_yellow_coin');
  },
 
  // Process a visitation with this village
  collect: function() {
    this.destroy();
    Crafty.audio.play('coin');
    Crafty.trigger('CoinCollected', this);
  }
});


// A village is a tile on the grid that the PC must visit in order to win the game
Crafty.c('RedCoin', {
  init: function() {
    this.requires('Actor, spr_red_coin');
  },
 
  // Process a visitation with this village
  collect: function() {
    this.destroy();
    Crafty.audio.play('coin');
    Crafty.trigger('CoinCollected', this);
  }
});


// A village is a tile on the grid that the PC must visit in order to win the game
Crafty.c('GreenCoin', {
  init: function() {
    this.requires('Actor, spr_green_coin');
  },
 
  // Process a visitation with this village
  collect: function() {
    this.destroy();
    Crafty.audio.play('coin');
    Crafty.trigger('CoinCollected', this);
  }
});


// A village is a tile on the grid that the PC must visit in order to win the game
Crafty.c('BlueCoin', {
  init: function() {
    this.requires('Actor, spr_blue_coin');
  },
 
  // Process a visitation with this village
  collect: function() {
    this.destroy();
    Crafty.audio.play('coin');
    Crafty.trigger('CoinCollected', this);
  }
});

// A Boots is a powerup that grants extra movement speed
Crafty.c('Boots', {
  init: function() {
    this.requires('Actor, spr_boots');
  },
  
  // Process a pickup of the boots
  collect: function() {
    this.destroy();
    
    Crafty.audio.play('speed');
    speedBoost = true;
    startBoost();
  },
});


// A clock is a tile that grants extra time
Crafty.c('Clock', {
  init: function() {
    this.requires('Actor, spr_clock');
  },
 
  // Process a pickup of the clock
  collect: function() {
    this.destroy();
    Crafty.audio.play('clocktick');
    clockPickup();
  }
});

// A flame is a tile that burns bushes
Crafty.c('Flame', {
  init: function() {
    this.requires('Actor, spr_flame');
  },
 
  // Process a pickup of the flame
  collect: function() {
    this.destroy();
    Crafty.audio.play('flamenoise');
    Crafty('Bush').destroy();
  }
});

// A multiplier is a tile that multiplies coins gathered
Crafty.c('Multiplier', {
  init: function() {
    this.requires('Actor, spr_coindouble');
  },
 
  // Process a pickup of the multiplier
  collect: function() {
    this.destroy();
    Crafty.audio.play('moneybooster');
    multiplierPickup(multiplierDuration,multiplierOn);
  }
});
